package com.ruix.app.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.ruix.app.R;
/**
 * Created by hadexs on 2017/4/30.
 */

public class CommonDialog extends Dialog implements View.OnClickListener{



    private CommonDialogAcitonListener listener;
    private TextView message;
    private TextView sureButton;
    private TextView cancelButton;

    public CommonDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_comm);
        setCanceledOnTouchOutside(true);
        initEvent();

        Window dialogWindow = getWindow();

        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;//宽高可设置具体大小
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
    }

    private void initEvent(){
        message = (TextView) findViewById(R.id.tv_common_message);
        sureButton = (TextView) findViewById(R.id.btn_sure);
        cancelButton = (TextView) findViewById(R.id.btn_cancel);
        sureButton.setOnClickListener(this);
        cancelButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_sure:
                dismiss();
                if (listener != null){
                    listener.positiveAction();
                }
                break;
            case R.id.btn_cancel:
                dismiss();
                if (listener != null){
                    listener.negertivieAction();
                }

                break;
        }
    }

    public void setListener(CommonDialogAcitonListener listener) {
        this.listener = listener;
    }


    public void show(String message){
        show();
        setMessage(message);
    }

    public void setPositiveString(String message){
        if (sureButton != null) {
            sureButton.setText(message);
        }
    }

    public void seCancelString(String message){
        if (cancelButton != null) {
            cancelButton.setText(message);
        }
    }
    public void setCancelStringColor(int color){
        if (cancelButton != null) {
            cancelButton.setTextColor(color);
        }
    }

    public void setMessage(String message) {
        if (this.message != null){
            this.message.setText(message);
        }
    }

    public interface CommonDialogAcitonListener{
        void positiveAction();
        void negertivieAction();
    }
}
